<?php
namespace Orm\UserAccount;

use Base\Exception\RmException;
class UserAccountClass extends \Base\BaseOrmObject{

    const SCORE_TYPE_TOTAL = 0; //总分
    const SCORE_TYPE_WEEK = 1; //周
    const ONE_WEEK_SCORE = 1; //周
    const TWO_WEEK_SCORE = 2; //周
    const THREE_WEEK_SCORE = 3; //周
    const FOUR_WEEK_SCORE = 4; //周
    public static function get_week($week_type){
        if($week_type == 1){
            return 'one_week_score';
        }
        if($week_type == 2){
            return 'two_week_score';
        }
        if($week_type == 3){
            return 'three_week_score';
        }
        if($week_type == 4){
            return 'four_week_score';
        }
        return 'one_week_score';
    }

    protected   $id,
				$uid = 0,
				$username = '', // 用户名
				$nickname = '', // 昵称
				$score = 0, // 总积分
				$one_week_score = 0, // 第一周积分
				$two_week_score = 0, // 第2周积分
				$three_week_score = 0, // 第3周积分
				$four_week_score = 0, // 第4周积分
				$play_times = 0, // 总投注
				$other_chance = 0, // 额外竞猜机会
				$is_del = 0,
				$insert_time = '',
				$update_time = '';

    function __init($data)
    {
        parent::__init($data);
        $this->dformat($data);
    }

    private function dformat()
    {
    }

    public function loadById($id = 0)
    {
        $model = new \Orm\UserAccount\UserAccountModel();
        $data = $model->loadById($id);
        self::__init($data);
    }

    public function loadByUserId($userid = '')
    {
        $model = new \Orm\UserAccount\UserAccountModel();
        $data = $model->getUserAccountByUid($userid);
        self::__init($data);
    }

    public function insert()
    {
        $this->checkNecessaryParam();
        $data = array(
            'uid' => $this->uid,
			'username' => $this->username,
			'nickname' => $this->nickname,
			'score' => $this->score,
			'one_week_score' => $this->one_week_score,
			'two_week_score' => $this->two_week_score,
			'three_week_score' => $this->three_week_score,
			'four_week_score' => $this->four_week_score,
			'play_times' => $this->play_times,
			'other_chance' => $this->other_chance,
        );

        $model = new \Orm\UserAccount\UserAccountModel();
        $model->insert($data);
        $this->id = $model->getLastId();
    }

    public function update()
    {
        $this->checkNecessaryParam();
        $data = array(
            'uid' => $this->uid,
			'username' => $this->username,
			'nickname' => $this->nickname,
			'score' => $this->score,
            'one_week_score' => $this->one_week_score,
            'two_week_score' => $this->two_week_score,
            'three_week_score' => $this->three_week_score,
            'four_week_score' => $this->four_week_score,
			'play_times' => $this->play_times,
			'other_chance' => $this->other_chance,
        );

        $model = new \Orm\UserAccount\UserAccountModel();
        $is_update = $model->update($data, "`id` = {$this->id}");
        return $is_update;
    }

    public function delete($id)
    {
        $data = array('is_del'=>1);
        $model = new \Orm\UserAccount\UserAccountModel();
        $model->update($data, "`id` = {$id}");
    }

    private function checkNecessaryParam()
    {
    }
}